/**  
 * 文 件 名: RoleAction.java  
 * 描    述：   
 * 版    权：Copyright (c)20012-2012 tongzhou All Rights Reserved.
 * 公    司: 同舟 
 * 作    者：彭定雄                     
 * 版    本: 1.0  
 * 创建时间: 2012-10-27
 *  
 * 修改历史：  
 * 时间                             作者                       版本                        描述  
 * ------------------------------------------------------------------  
 * 2012-10-27        彭定雄                1.0          1.0 Version  
 */         

/**
 * 
 */
package com.tongzhou.system.rightsMgmt.action;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.tongzhou.db.exception.DBException;
import com.tongzhou.system.menu.dao.IMenuDAO;
import com.tongzhou.system.menu.model.Menu;
import com.tongzhou.system.rightsMgmt.dao.IGroupsDAO;
import com.tongzhou.system.rightsMgmt.dao.IRightDAO;
import com.tongzhou.system.rightsMgmt.dao.IRightTypeDAO;
import com.tongzhou.system.rightsMgmt.dao.IRoleDAO;
import com.tongzhou.system.rightsMgmt.model.Groups;
import com.tongzhou.system.rightsMgmt.model.Right;
import com.tongzhou.system.rightsMgmt.model.RightType;
import com.tongzhou.system.rightsMgmt.model.Role;
import com.tongzhou.system.rightsMgmt.model.RoleRightTypeRelation;
import com.tongzhou.system.rightsMgmt.model.SearchRoleCondition;
import com.tongzhou.system.userMgr.dao.IUserMgrDAO;
import com.tongzhou.system.userMgr.model.User;
import com.tongzhou.util.Util;

public class RoleAction extends ActionSupport{
	private String id;//
	private String name;//角色名称
	private String active;//是否可用(Y：可用;N：不可用)
	private String create_time;//创建时间
	private String create_by;//创建人
	private String update_time;//更新时间
	private String update_by;//更新人
	
	private Role role;
	private List<Role> result;
	private IRoleDAO dao;
	private int pageSize ;
	private int currentPage ;
	private String orderBy ;
	private String orderByType ;
	private SearchRoleCondition condition;
	private String judgment;
	private List<Menu> menuList;
	private List<Menu> roleMenuList;
	private List<Right> rightList;
	private List<Right> roleRightList;
	private List<User> userList;
	private List<User> roleUserList;
	private List<Role> roleList;
	private List<Role> userRoleList;
	private List<Groups> roleGroupsList;
	private List<Groups> groupsList;
	private List<RoleRightTypeRelation> rtDataList;
	private List<RoleRightTypeRelation> roleRTDataList;
	private List<RightType> rightTypeList;
	private List<RightType> roleRightTypeList;
	private IMenuDAO menuDAO;
	private IRightDAO rightDAO;
	private IUserMgrDAO userDAO;
	private IGroupsDAO groupsDAO;
	private IRightTypeDAO rightTypeDAO;
	private Menu menu;
	private Right right;
	private User user;
	private String userID;
	private RoleRightTypeRelation roleRTRelation;
	
	private String roleMenuStr;
	private String childRole;
	private String itemsVal;
	private String rtDataStr;
	private String table_name;
	private String rightTypeID;
	
	public String listRole() throws DBException{
		checkSearchCondition();
		condition = new SearchRoleCondition();
		if (currentPage == 0){
			currentPage = 1;
		}
		condition.setName(name);
		condition.setActive(active);
		condition.setCurrentPage(currentPage);
		condition.setOrderBy(orderBy);
		condition.setOrderByType(orderByType);
		condition.setPageSize(pageSize);
	    result = dao.listRole(condition);
		return "listRole";
	}
	/**
	 * 设置列表显示格式
	 *@author: 彭定雄
	 */
	private void checkSearchCondition(){
		if(currentPage <=0){
			currentPage = 1 ;
		}
		
		if(pageSize<=0){
			pageSize = 10 ;
		}
		
		if(orderBy==null||"".equals(orderBy)){
			orderBy = "r.id";
		}
		
		if(orderByType == null || "".equals(orderByType)){
			orderByType = "asc" ;
		}
	}
	
	public String toAddRole() throws DBException{
		return "toAddRole";
	}
	
	public String checkRoleByName() throws DBException{
		role=new Role();
		try {
			role.setName(new String(name.getBytes("ISO-8859-1"),"UTF-8"));
			role.setId(id);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		result=dao.checkRoleByName(role);
		if(result.size()>0){
			judgment="0";
		}else{
			judgment="1";
		}
		return SUCCESS;
	}
	
	public String addRole() throws DBException{
		role=new Role();
		role.setName(name);
		role.setActive(active);
		role.setCreate_by(String.valueOf(Util.getAdminUserId()));
		dao.addRole(role);
		listRole();
		return "listRole";
	}
	
	public String deleteRole() throws DBException{
		role=new Role();
		role.setId(id);
		
		dao.deleteRole(role);
		listRole();
		name="";
		return "listRole";
	}
	
	public String toEditRole() throws DBException{
		role=new Role();
		role.setId(id);
		role=dao.getRoleById(role);
		return "toEditRole";
	}
	
	public String editRole() throws DBException{
		role=new Role();
		role.setId(id);
		role.setName(name);
		role.setActive(active);
		role.setUpdate_by(String.valueOf(Util.getAdminUserId()));
		dao.updateRole(role);
		listRole();
		return "listRole";
	}
	
	public String toEditRoleMenu() throws DBException{
		role=new Role();
		role.setId(id);
		//获取可以看到的菜单
		roleMenuList=dao.getRoleMenuById(role);
		roleMenuStr="";
		for(Menu m:roleMenuList){
			if(roleMenuStr!=""){
				roleMenuStr+="`";
			}
			roleMenuStr+=m.getId();
		}
		//查出全部菜单
		List<String> roles=Util.getAdminUserRoles();
		if(roles.contains("1")){
			List<Menu> ml=menuDAO.listMenu();
			menuList=new ArrayList<Menu>();
			for(Menu menu:ml){
				List<Menu> subMenuList=menuDAO.getSubMenuList(menu);
				menu.setList(subMenuList);
				menuList.add(menu);
			}
		}else{
			List<Menu> ml=menuDAO.listMenu2();
			menuList=new ArrayList<Menu>();
			for(Menu menu:ml){
				List<Menu> subMenuList=menuDAO.getSubMenuList2(menu);
				menu.setList(subMenuList);
				menuList.add(menu);
			}
		}
		return "toEditRoleMenu";
	}
	
	public String editRoleMenu() throws DBException{
		role=new Role();
		role.setId(id);
		role.setItemsVal(itemsVal);
		dao.updateRoleMenu(role);
		listRole();
		return "listRole";
	}
	
	public String changeChildNode() throws DBException{
		menu=new Menu();
		menu.setId(id);
		menuList=menuDAO.getSubMenuList(menu);
		childRole="";
		for(Menu m:menuList){
			if(childRole!=""){
				childRole+="`";
			}
			childRole+=m.getId();
		}
		return SUCCESS;
	}
	
	public String toEditRoleRight() throws DBException{
		role=new Role();
		role.setId(id);
		roleRightList=dao.getRoleRightById(role);
		roleMenuStr="";
		for(Right r:roleRightList){
			if(roleMenuStr!=""){
				roleMenuStr+="`";
			}
			roleMenuStr+=r.getId();
		}
		right=new Right();
		right.setParent_id("0");
		List<String> roles=Util.getAdminUserRoles();
		if(roles.contains("1")){
			List<Right> rr=rightDAO.getRightListByParentID(right);
			rightList=new ArrayList<Right>();
			for(Right r1:rr){
				Right r=getChildRight(r1);
				rightList.add(r);
			}
		}else{
			List<Right> rr=rightDAO.getRightListByParentID(right);
			rightList=new ArrayList<Right>();
			for(Right r1:rr){
				Right r=getChildRight(r1);
				rightList.add(r);
			}
		}
		return "toEditRoleRight";
	}
	
	//查出子记录
	public Right getChildRight(Right r){
		List<Right> rlist=null;
		try {
			Right r1=new Right();
			r1.setParent_id(r.getId());
			rlist=rightDAO.getRightListByParentID(r1);
		} catch (DBException e) {
			e.printStackTrace();
		}
		if(rlist.size()>0){
			List<Right> rlist2=new ArrayList<Right>();
			for(Right sr:rlist){
				sr=getChildRight(sr);
				rlist2.add(sr);
			}
			r.setList(rlist2);
		}
		return r;
	}
	
	//加角色后查出子记录
	public Right getChildRight2(Right r){
		List<Right> rlist=null;
		try {
			Right r1=new Right();
			r1.setParent_id(r.getId());
			rlist=rightDAO.getRightListByParentID2(r1);
		} catch (DBException e) {
			e.printStackTrace();
		}
		if(rlist.size()>0){
			List<Right> rlist2=new ArrayList<Right>();
			for(Right sr:rlist){
				sr=getChildRight(sr);
				rlist2.add(sr);
			}
			r.setList(rlist2);
		}
		return r;
	}
	
	public String editRoleRight() throws DBException{
		role=new Role();
		role.setId(id);
		role.setItemsVal(itemsVal);
		dao.updateRoleRight(role);
		listRole();
		return "listRole";
	}
	
	public String changeChildRNode() throws DBException{
		right=new Right();
		right.setParent_id(id);
		List<Right> rr=rightDAO.getRightListByParentID(right);
		rightList=new ArrayList<Right>();
		for(Right r1:rr){
			Right r=getChildRight(r1);
			rightList.add(r);
		}
		childRole="";
		for(Right r:rightList){
			if(childRole!=""){
				childRole+="`";
			}
			childRole+=r.getId();
			if(r.getList()!=null){
				for(Right r2:r.getList()){
					if(childRole!=""){
						childRole+="`";
					}
					childRole+=r2.getId();
					if(r2.getList()!=null){
						for(Right r3:r2.getList()){
							if(childRole!=""){
								childRole+="`";
							}
							childRole+=r3.getId();
							if(r3.getList()!=null){
								for(Right r4:r3.getList()){
									if(childRole!=""){
										childRole+="`";
									}
									childRole+=r4.getId();
								}
							}
							
						}
					}
				}
			}
		}
		return SUCCESS;
	}
	
	public String toEditRoleUser() throws DBException{
		role=new Role();
		role.setId(id);
		roleUserList=dao.getRoleUserById(role);
		
		userList=userDAO.getAllUser();
		return "toEditRoleUser";
	}
	
	public String editRoleUser() throws DBException{
		role=new Role();
		role.setId(id);
		role.setItemsVal(itemsVal);
		dao.updateRoleUser(role);
		listRole();
		return "listRole";
	}
	
	public String toEditRoleGroups() throws DBException{
		role=new Role();
		role.setId(id);
		roleGroupsList=dao.getRoleGroupsById(role);
		
		groupsList=groupsDAO.getAllGroups();
		return "toEditRoleGroups";
	}
	
	public String editRoleGroups() throws DBException{
		role=new Role();
		role.setId(id);
		role.setItemsVal(itemsVal);
		dao.updateRoleGroups(role);
		listRole();
		return "listRole";
	}
	
//	public String toEditRoleRightType() throws DBException{
//		role=new Role();
//		role.setId(id);
//		rightTypeList=rightTypeDAO.getAllRightType();
//		List<RightType> rts=dao.getRoleRTById(role);
//		roleRightTypeList=new ArrayList<RightType>();
//		for(RightType rt:rts){
//			rtDataList=dao.getRTDataById(rt);
//			roleRTDataList=dao.getRoleRTDataById(rt);
//			//相应的权限类型对应的全部数据
//			rt.setAllList(rtDataList);
//			//角色对应的权限类型数据
//			rt.setList(roleRTDataList);
//			roleRightTypeList.add(rt);
//		}
//		return "toEditRoleRithtType";
//	}
	
	public String getRTData() throws DBException{
		roleRTRelation=new RoleRightTypeRelation();
		roleRTRelation.setRole_id(id);
		roleRTRelation.setId(rightTypeID);
		if(Util.checkObjIsNull(table_name)){
			return ERROR;
		}else{
			roleRTRelation.setTable_name(table_name);
		}
		rtDataList=dao.getRTData(roleRTRelation);
		rtDataStr="";
		for(RoleRightTypeRelation m:rtDataList){
			if(!rtDataStr.equals("")){
				rtDataStr+=";";
			}
			rtDataStr+=m.getDataID()+":"+m.getDataName();
		}
		return SUCCESS;
	}
	
	public String editRoleRithtType() throws DBException{
		role=new Role();
		role.setId(id);
		role.setItemsVal(itemsVal);
		dao.updateRoleRightType(role);
		listRole();
		return "listRole";
	}
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getActive() {
		return active;
	}
	public void setActive(String active) {
		this.active = active;
	}
	public String getCreate_time() {
		return create_time;
	}
	public void setCreate_time(String createTime) {
		create_time = createTime;
	}
	public String getCreate_by() {
		return create_by;
	}
	public void setCreate_by(String createBy) {
		create_by = createBy;
	}
	public String getUpdate_time() {
		return update_time;
	}
	public void setUpdate_time(String updateTime) {
		update_time = updateTime;
	}
	public String getUpdate_by() {
		return update_by;
	}
	public void setUpdate_by(String updateBy) {
		update_by = updateBy;
	}
	public Role getRole() {
		return role;
	}
	public void setRole(Role role) {
		this.role = role;
	}
	public List<Role> getResult() {
		return result;
	}
	public void setResult(List<Role> result) {
		this.result = result;
	}
	public IRoleDAO getDao() {
		return dao;
	}
	public void setDao(IRoleDAO dao) {
		this.dao = dao;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public String getOrderBy() {
		return orderBy;
	}
	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}
	public String getOrderByType() {
		return orderByType;
	}
	public void setOrderByType(String orderByType) {
		this.orderByType = orderByType;
	}
	public SearchRoleCondition getCondition() {
		return condition;
	}
	public void setCondition(SearchRoleCondition condition) {
		this.condition = condition;
	}
	public String getJudgment() {
		return judgment;
	}
	public void setJudgment(String judgment) {
		this.judgment = judgment;
	}
	public List<Menu> getMenuList() {
		return menuList;
	}
	public void setMenuList(List<Menu> menuList) {
		this.menuList = menuList;
	}
	public List<Right> getRightList() {
		return rightList;
	}
	public void setRightList(List<Right> rightList) {
		this.rightList = rightList;
	}
	public IMenuDAO getMenuDAO() {
		return menuDAO;
	}
	public void setMenuDAO(IMenuDAO menuDAO) {
		this.menuDAO = menuDAO;
	}
	public IRightDAO getRightDAO() {
		return rightDAO;
	}
	public void setRightDAO(IRightDAO rightDAO) {
		this.rightDAO = rightDAO;
	}
	public List<Menu> getRoleMenuList() {
		return roleMenuList;
	}
	public void setRoleMenuList(List<Menu> roleMenuList) {
		this.roleMenuList = roleMenuList;
	}
	public List<Right> getRoleRightList() {
		return roleRightList;
	}
	public void setRoleRightList(List<Right> roleRightList) {
		this.roleRightList = roleRightList;
	}
	public String getItemsVal() {
		return itemsVal;
	}
	public void setItemsVal(String itemsVal) {
		this.itemsVal = itemsVal;
	}
	public String getRoleMenuStr() {
		return roleMenuStr;
	}
	public void setRoleMenuStr(String roleMenuStr) {
		this.roleMenuStr = roleMenuStr;
	}
	public String getChildRole() {
		return childRole;
	}
	public void setChildRole(String childRole) {
		this.childRole = childRole;
	}
	public Menu getMenu() {
		return menu;
	}
	public void setMenu(Menu menu) {
		this.menu = menu;
	}
	public Right getRight() {
		return right;
	}
	public void setRight(Right right) {
		this.right = right;
	}
	public IUserMgrDAO getUserDAO() {
		return userDAO;
	}
	public void setUserDAO(IUserMgrDAO userDAO) {
		this.userDAO = userDAO;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public List<User> getUserList() {
		return userList;
	}
	public void setUserList(List<User> userList) {
		this.userList = userList;
	}
	public List<User> getRoleUserList() {
		return roleUserList;
	}
	public void setRoleUserList(List<User> roleUserList) {
		this.roleUserList = roleUserList;
	}
	public List<Role> getRoleList() {
		return roleList;
	}
	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}
	public List<Role> getUserRoleList() {
		return userRoleList;
	}
	public void setUserRoleList(List<Role> userRoleList) {
		this.userRoleList = userRoleList;
	}
	public String getUserID() {
		return userID;
	}
	public void setUserID(String userID) {
		this.userID = userID;
	}
	public List<Groups> getRoleGroupsList() {
		return roleGroupsList;
	}
	public void setRoleGroupsList(List<Groups> roleGroupsList) {
		this.roleGroupsList = roleGroupsList;
	}
	public List<Groups> getGroupsList() {
		return groupsList;
	}
	public void setGroupsList(List<Groups> groupsList) {
		this.groupsList = groupsList;
	}
	public IGroupsDAO getGroupsDAO() {
		return groupsDAO;
	}
	public void setGroupsDAO(IGroupsDAO groupsDAO) {
		this.groupsDAO = groupsDAO;
	}
	public IRightTypeDAO getRightTypeDAO() {
		return rightTypeDAO;
	}
	public void setRightTypeDAO(IRightTypeDAO rightTypeDAO) {
		this.rightTypeDAO = rightTypeDAO;
	}
	public List<RoleRightTypeRelation> getRtDataList() {
		return rtDataList;
	}
	public void setRtDataList(List<RoleRightTypeRelation> rtDataList) {
		this.rtDataList = rtDataList;
	}
	public List<RightType> getRightTypeList() {
		return rightTypeList;
	}
	public void setRightTypeList(List<RightType> rightTypeList) {
		this.rightTypeList = rightTypeList;
	}
	public String getRtDataStr() {
		return rtDataStr;
	}
	public void setRtDataStr(String rtDataStr) {
		this.rtDataStr = rtDataStr;
	}
	public RoleRightTypeRelation getRoleRTRelation() {
		return roleRTRelation;
	}
	public void setRoleRTRelation(RoleRightTypeRelation roleRTRelation) {
		this.roleRTRelation = roleRTRelation;
	}
	public String getTable_name() {
		return table_name;
	}
	public void setTable_name(String tableName) {
		table_name = tableName;
	}
	public String getRightTypeID() {
		return rightTypeID;
	}
	public void setRightTypeID(String rightTypeID) {
		this.rightTypeID = rightTypeID;
	}
	public List<RoleRightTypeRelation> getRoleRTDataList() {
		return roleRTDataList;
	}
	public void setRoleRTDataList(List<RoleRightTypeRelation> roleRTDataList) {
		this.roleRTDataList = roleRTDataList;
	}
	public List<RightType> getRoleRightTypeList() {
		return roleRightTypeList;
	}
	public void setRoleRightTypeList(List<RightType> roleRightTypeList) {
		this.roleRightTypeList = roleRightTypeList;
	}
}
